package com.ldw.demo.controller;

import com.ldw.demo.pojo.Provider;
import com.ldw.demo.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@Transactional
public class ProviderController {
    @Autowired
    private ProviderService providerService;

    @GetMapping("/jsp/provider.do")
    public String queryProvider(Model model, @Nullable @RequestParam("queryProCode") String queryProCode,
                                @Nullable @RequestParam("queryProName") String queryProName,
                                @Nullable @RequestParam("method") String method,
                                @Nullable @RequestParam("proid") String id
    ) {
        if ("query".equals(method) || method == null) {
            List<Provider> providerList = providerService.queryProvider(queryProCode, queryProName);
            model.addAttribute("providerList", providerList);
            model.addAttribute("queryProCode", queryProCode);
            model.addAttribute("queryProName", queryProName);
        } else if ("modify".equals(method)) {
            Provider provider = providerService.queryProviderById(Integer.parseInt(id));
            model.addAttribute("provider", provider);
            return "providermodify";
        } else if("delprovider".equals(method)) {
            providerService.deleteProvider(Integer.parseInt(id));
        }
        return "providerlist";
    }

    @PostMapping("/jsp/provider.do")
    public String addProvider(HttpServletRequest request) {
        HashMap map = new HashMap();
        String method = request.getParameter("method");
        String proCode = request.getParameter("proCode");
        String proName = request.getParameter("proName");
        String proContact = request.getParameter("proContact");
        String proPhone = request.getParameter("proPhone");
        String proAddress = request.getParameter("proAddress");
        String proFax = request.getParameter("proFax");
        String proDesc = request.getParameter("proDesc");
        map.put("proCode", proCode);
        map.put("proName", proName);
        map.put("proContact", proContact);
        map.put("proPhone", proPhone);
        map.put("proAddress", proAddress);
        map.put("proFax", proFax);
        map.put("proDesc", proDesc);
        map.put("creationDate", new Date());
        if ("add".equals(method)) {
            providerService.addProvider(map);
        }
        if (method == null) {
            providerService.updateProvider(map);
        }
        return "redirect:/jsp/provider.do";
    }
}
